package com.whfc.mach.manager.strategy.work;

import com.whfc.mach.dto.AppMachDaySegDTO;
import com.whfc.mach.enums.WorkState;
import com.whfc.mach.manager.util.MachSegmentUtil;
import com.whfc.mach.manager.util.MachUtil;

import java.util.List;

/**
 * @Description: 葛洲坝项目-工时修正策略
 * 规则: 停机-怠机-停机-怠机-停机,连续的序列,合并成停机
 * @author: xugcheng
 * @version: 1.0
 * @date: 2021-06-09 14:26
 */
public class CggcWorkStrategy implements WorkStrategy {

    /**
     * 最小分段数量
     */
    private static int MIN_SEG_SIZE = 5;

    @Override
    public Integer amend(Integer workState) {
        return workState;
    }

    @Override
    public List<AppMachDaySegDTO> amend(List<AppMachDaySegDTO> segmentList) {

        if (segmentList.size() >= MIN_SEG_SIZE) {
            for (int i = 0; i <= segmentList.size() - MIN_SEG_SIZE; i++) {
                AppMachDaySegDTO seg0 = segmentList.get(i);
                AppMachDaySegDTO seg1 = segmentList.get(i + 1);
                AppMachDaySegDTO seg2 = segmentList.get(i + 2);
                AppMachDaySegDTO seg3 = segmentList.get(i + 3);
                AppMachDaySegDTO seg4 = segmentList.get(i + 4);
                //满足"停-怠-停-怠-停"
                if (MachUtil.isStopState(seg0.getWorkState())
                        && MachUtil.isIdleState(seg1.getWorkState())
                        && MachUtil.isStopState(seg2.getWorkState())
                        && MachUtil.isIdleState(seg3.getWorkState())
                        && MachUtil.isStopState(seg4.getWorkState())) {
                    //合并成新分段
                    MachSegmentUtil.buildMachDaySegment(seg0, seg0.getStartTime(), seg4.getEndTime(), WorkState.STOP);
                    //删除被合并的分段
                    segmentList.remove(i + 1);
                    segmentList.remove(i + 1);
                    segmentList.remove(i + 1);
                    segmentList.remove(i + 1);
                    i--;
                }
            }
        }

        return segmentList;
    }
}
